package com.bangyu.monitor.core.util;

import com.bangyu.monitor.core.common.constant.Const;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.influxdb.InfluxDB;
import org.influxdb.dto.BatchPoints;
import org.influxdb.dto.Point;
import org.influxdb.dto.Query;
import org.influxdb.dto.QueryResult;
import org.springframework.context.annotation.Configuration;

import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @Auther: zhangxue
 * @Date: 19/6/25 21:23
 * @Description:
 */
@Slf4j
@Configuration
public class InfluxDbUtil {





    public static InfluxDB getInfluxDB(){
        return SpringContextUtil.getBean(InfluxDB.class);
    }

    /**
     * 设置数据保存策略
     * defalut 策略名 /database 数据库名/ 30d 数据保存时限30天/ 1  副本个数为1/ 结尾DEFAULT 表示 设为默认的策略
     */
    public static void createRetentionPolicy(String database){
        String command = String.format("CREATE RETENTION POLICY \"%s\" ON \"%s\" DURATION %s REPLICATION %s DEFAULT",
                "defalut", Const.INFLUX_DATABASE, "30d", 1);
        query(database,command);
    }

    /**
     * 创建自定义保留策略
     *
     * @param policyName
     *            策略名
     * @param duration
     *            保存天数
     * @param replication
     *            保存副本数量
     * @param isDefault
     *            是否设为默认保留策略
     */
    public static void createRetentionPolicy(String database,String policyName, String duration, int replication, Boolean isDefault) {
        String sql = String.format("CREATE RETENTION POLICY \"%s\" ON \"%s\" DURATION %s REPLICATION %s ",
                policyName, Const.INFLUX_DATABASE, duration, replication);
        if (isDefault) {
            sql = sql + " DEFAULT";
        }
        query(database,sql);
    }

    /**
     * 查询
     * @param command 查询语句
     * @return
     */
    public static QueryResult query(String database,String command){
        return getInfluxDB().query(new Query(command, database));
    }

    public static void insert(String database,String policy,Point point){
        getInfluxDB().write(database,policy,point);
    }


    public static   void batchInsert(BatchPoints points){
        getInfluxDB().write(points);
    }

    public static BatchPoints createBatchPoints(String dataSource){
        return BatchPoints.database(dataSource).build();
    }
    public static BatchPoints createBatchPoints(String dataSource,String policy){
        return BatchPoints.database(dataSource).retentionPolicy(policy).build();
    }
    public static Point createPoint(String meraurement,Map<String,String> tags,Map<String,Object> fields,long time){
        return Point.measurement(meraurement).
                tag(tags).fields(fields).
                time(time,TimeUnit.MILLISECONDS)
                .build();
    }



    /**
     * 删除
     * @param command 删除语句
     * @return 返回错误信息
     */
    public static String deleteMeasurementData(String database,String command){
        QueryResult result = getInfluxDB().query(new Query(command, database));
        return result.getError();
    }

    /**
     * 创建数据库
     * @param dbName
     */
    public static void createDB(String dbName){
        getInfluxDB().createDatabase(dbName);
    }

    /**
     * 删除数据库
     * @param dbName
     */
    public static void deleteDB(String dbName){
        getInfluxDB().deleteDatabase(dbName);
    }

    public static QueryResult.Series getSeries(QueryResult queryResult){
        if(!queryResult.hasError() && CollectionUtils.isNotEmpty(queryResult.getResults())){
            QueryResult.Result result = queryResult.getResults().get(0);
            List<QueryResult.Series> serieslist = result.getSeries();
            if(CollectionUtils.isNotEmpty(serieslist)){
                return serieslist.get(0);

            }
        }
        return null;
    }



}
